<template>
  <div :ref="className" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons')
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      handler(val) {
        this.setOptions(val)
      },
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  // 销毁实例
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      // 获取实例
      this.chart = echarts.init(this.$refs[this.className], 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      const options = {
        grid: {
          left: 10,
          right: 10,
          bottom: 10,
          top: 30,
          containLabel: true
        },
        legend: {
          show: true,
          formatter: (param) => {
            const obj = {
              expected: '期望',
              actual: '实际'
            }
            return obj[param]
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
          axisLine: {
            show: true
          },
          axisTick: {
            show: false
          },
          boundaryGap: false
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: true
          },
          axisTick: {
            show: false
          }
        },
        series: [
          {
            name: 'expected',
            type: 'line',
            data: expectedData,
            cursor: 'pointer',
            itemStyle: {
              // 拐点颜色
              color: '#FF005A',
              borderWidth: 0,
              // 线条颜色和宽度
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            },
            symbolSize: 5,
            smooth: true,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: 'actual',
            type: 'line',
            data: actualData,
            smooth: true,
            itemStyle: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              }
            },
            areaStyle: {
              color: 'rgba(56, 136, 250, .1)'
            },
            symbolSize: 5,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }
        ]
      }
      this.chart.setOption(options)
    }
  }
}
</script>
